package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.BRD_GRA;
import cn.edu.nju.software.cdiy.bean.BRD_MOUSE;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.Mouse;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.MouseDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortByPriceAsc;
import cn.edu.nju.software.cdiy.util.SortByPriceDesc;
import cn.edu.nju.software.cdiy.util.SortByWScoreAsc;
import cn.edu.nju.software.cdiy.util.SortByWScoreDesc;
import cn.edu.nju.software.cdiy.util.SortByZScoreAsc;
import cn.edu.nju.software.cdiy.util.SortByZScoreDesc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class MouseDaoImpl implements MouseDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Mouse> getMouseList(int page, SortType s, HardwareSet hs,
			SortWay sw) {
		return null;
	}

	@Override
	public List<Hardware> getNewestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Mouse ORDER BY INSTORAGETIME DESC";
		List<Mouse> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Mouse";
		List<Mouse> list = sess.createQuery(sql).list();

		Collections.sort(list);

		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_MOUSE";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_MOUSE> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		
		return returnList;
	}

	@Override
	public List<Mouse> filter(int page,Map<String, List<Integer>> map, SortType st,SortWay sw,HardwareSet hs) {
//		// 搜索时的过滤方法
//		if(map==null)
//			return getMouseList(1, st, null, sw);
//
//		// 获得此产品表
//		List<Mouse> hwList = getFilterList(l, st, sw);
//
//		// 最后的匹配表
//		List<Mouse> matchList = new ArrayList<Mouse>();
//
//		// 开始过滤
//		Set<String> keys = map.keySet();
//		for (String s : keys) {
//			if (s.equals("品牌")) {
//				// 品牌过滤
//				List<String> list = map.get(s);
//
//				for (int j = 0; j < hwList.size(); j++) {
//					for (int k = 0; k < list.size(); k++) {
//						if (hwList.get(j).getBrand().getBrand()
//								.equals(list.get(0))) {
//							matchList.add(hwList.get(j));
//							break;
//						}
//					}
//				}
//				continue;
//			} else if (s.equals("价格")) {
//				List<String> list = map.get(s);
//
//				for (int j = 0; j < hwList.size(); j++) {
//					for (int k = 0; k < list.size(); k++) {
//						if (Changer.checkPrice(hwList.get(j), list.get(0))) {
//							matchList.add(hwList.get(j));
//							break;
//						}
//					}
//				}
//				continue;
//			}
//			hwList = matchList;
//			matchList = new ArrayList<Mouse>();
//		}
//		int maxNum = Pager.getMax(page);
//		int minNum = Pager.getMin(maxNum);
//		matchList=new Pager<Mouse>().getPageList(matchList,
//				minNum, maxNum);
//		return matchList;
		return null;
	}

	private List<Mouse> getFilterList(List<Mouse> l, SortType st, SortWay sw) {
		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:
			if (l == null) {
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					sql = "FROM Mouse ORDER BY OPRICE ASC";
				else
					sql = "FROM Mouse ORDER BY OPRICE DESC";
				l = sess.createQuery(sql).list();
			}else{
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					Collections.sort(l, new SortByPriceAsc());
				else
					Collections.sort(l, new SortByPriceDesc());
			}
			break;
		case W_SCORE:
			if (l == null) {
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Mouse ORDER BY WSCORE DESC";
				else
					sql = "FROM Mouse ORDER BY WSCORE ASC";
				l = sess.createQuery(sql).list();
			}else{
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					Collections.sort(l, new SortByWScoreAsc());
				else
					Collections.sort(l, new SortByWScoreDesc());
			}
			break;
		case Z_SCORE:
			if (l == null) {
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Mouse ORDER BY ZSCORE DESC";
				else
					sql = "FROM Mouse ORDER BY ZSCORE ASC";
				l = sess.createQuery(sql).list();
			}else{
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					Collections.sort(l, new SortByZScoreAsc());
				else
					Collections.sort(l, new SortByZScoreDesc());
			}
			break;
		case NEW:
			if (l == null) {
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Mouse ORDER BY INSTORAGETIME DESC";
				else
					sql = "FROM Mouse ORDER BY INSTORAGETIME ASC";
				l = sess.createQuery(sql).list();
			}else{
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					Collections.sort(l, new SortByZScoreAsc());
				else
					Collections.sort(l, new SortByZScoreDesc());
			}
			break;
		case HOT:
			if (l == null) {
				sql = "FROM Mouse";
				l = sess.createQuery(sql).list();
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					Collections.sort(l);
				else {
					Collections.sort(l,new SortByHotAsc());
				}
			}else{
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					Collections.sort(l, new SortByZScoreAsc());
				else
					Collections.sort(l, new SortByZScoreDesc());
			}
			break;
		default:
			break;

		}
		
		return l;
	}

	@Override
	public List<Mouse> search(String keywords,int page) {
		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM Mouse";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<Mouse> list = sess.createQuery(sql).list();

		List<Mouse> returnList = new Pager<Mouse>().getPageList(list,
				minNum, maxNum);

		
		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {
//		HashMap<String, List<String>> map = new HashMap<String, List<String>>();
//		map.put(getBrandItem().getItemName(), getBrandItem()
//				.getItemChoiceList());
//		map.put(getPriceItem().getItemName(), getPriceItem()
//				.getItemChoiceList());
//
//		return map;
		return null;
	}

	private FilterItem getPriceItem() {
		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName("价格");

		List<String> choices = new ArrayList<String>();
		choices.add("300元以下");
		choices.add("300-400元");
		choices.add("400-500元");
		choices.add("500-600元");
		choices.add("600-800元");
		choices.add("800-1000元");
		choices.add("1000-1500元");
		choices.add("1500-2000元");
		choices.add("2000元以上");

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName("品牌");

		String sql = "FROM BRD_GRA";
		List<BRD_GRA> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);
		
		return fi;
	}

	@Override
	public Mouse getOneById(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Mouse c = (Mouse) sess.load(Mouse.class, id);
		
		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {
		return null;
	}
	
	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		return false;
	}

	private Value getValueByName(String name){
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='"+name+"'";
		List<Value> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}
	
	private Interface getInterfaceByName(String name){
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='"+name+"'";
		List<Interface> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	@Override
	public void save(Mouse t) {
		Session sess = sessionFactory.getCurrentSession();
//		Transaction tran = sess.beginTransaction();
		sess.saveOrUpdate(t);
		sess.flush();
		
	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {
		return 0;
	}

	@Override
	public int getListPageNum(HardwareSet hs) {
		return 0;
	}

	@Override
	public List<Mouse> getOneBySupplierID(int id) {
		return null;
	}
}
